package cn.cloud.mqtt2redis.controller;

import cn.cloud.mqtt2redis.service.MqttPublish;
import cn.cloud.mqtt2redis.service.RedisService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Timer;
import java.util.TimerTask;

@Service
@EnableAsync
public class ScheduleTask {
    @Autowired
    private MqttPublish mqttPublish;
    @Value("${jedis.pool.database}") private String database;
    private static boolean isDeviceDiscovery = false;
    private static boolean isDeviceProbeParse = false;
    private static boolean isTimerStart = false;

    @Scheduled(cron = "*/3 * * * * ?")
    public void pushDataScheduled(){
        //程序启动，获取设备列表
        if(!this.isDeviceDiscovery){
            mqttPublish.device_discovery();
            this.isDeviceDiscovery = true;
        }
        if(!RedisService.isMachineIdToRedisCompleted) return;//等待设备列表写入完毕

        //遍历获取设备的probe文件
        if(this.isDeviceDiscovery && !this.isDeviceProbeParse){
            mqttPublish.publish_multi_device_probe();
            this.isDeviceProbeParse = true;
        }
        if(!mqttPublish.isGetProbeCompleted) return;  //等待所有设备的probe文件请求发布

        //启动定时器,定时获取GFILE
        if(this.isDeviceDiscovery && this.isDeviceProbeParse && !this.isTimerStart){
            Timer GFile_timer = new Timer();
            GFile_timer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mqttPublish.request_progMap();
                }
            },1500,15*60*1000);
            this.isTimerStart = true;
			
        }
    }
}
